package org.tom.pet.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.tom.basic.service.impl.BaseServiceImpl;
import org.tom.basic.util.DistanceUtil;
import org.tom.basic.util.PageList;
import org.tom.basic.util.Point;
import org.tom.org.domain.Employee;
import org.tom.org.domain.Shop;
import org.tom.org.mapper.EmployeeMapper;
import org.tom.org.mapper.ShopMapper;
import org.tom.pet.domain.Pet;
import org.tom.pet.domain.SearchMasterMsg;
import org.tom.pet.mapper.SearchMasterMsgMapper;
import org.tom.pet.query.SearchMasterMsgQuery;
import org.tom.pet.service.IPetService;
import org.tom.pet.service.ISearchMasterMsgService;
import org.tom.user.domain.Logininfo;
import org.tom.user.domain.User;
import org.tom.user.mapper.UserMapper;

import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private IPetService petService;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    /**
     * 寻主发布的业务层
     * @param smm
     * @param logininfo
     */
    @Override
    public void publish(SearchMasterMsg smm, Logininfo logininfo) {
        // 将传过来的用户信息获取用户id
        User user = userMapper.findByLogininfo(logininfo.getId());
        // 将用户信息添加到寻主的对象
        smm.setUser(user);
        // 将用户信息的id设置到寻主对象，由于方便后面的使用，就将对象和id都加入到smm中间
        smm.setUser_id(user.getId());
        // 同伙寻主对象中的地址获取地图上经纬度的点
        Point point = DistanceUtil.getPoint(smm.getAddress());
        // 获取所有店铺的信息
        List<Shop> shops = shopMapper.loadAll();
        // 调用工具类，输入之前获取的定位信息和所有店铺找到最近店铺
        Shop shop = DistanceUtil.getNearestShop(point, shops);
        // 将最近的店铺信息添加到寻主的对象中
        smm.setShop(shop);
        // 将最近的店铺id设置到寻主的对象中，方便之后的使用就都填到对象之中
        smm.setShop_id(shop.getId());
        // 将寻主的对象中添加到数据库
        searchMasterMsgMapper.add(smm);
    }

    /**
     * 待处理宠物信息的查询
     * @param searchMasterMsgQuery
     * @param logininfoId
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery searchMasterMsgQuery, Long logininfoId) {
        // 通过用户信息的id查询到用户
        Employee employee = employeeMapper.findByLogininfoId(logininfoId);
        // 通过判断用户的shop_id是否为空来判断目前登录的用户是平台员工还是店铺员工
        if (employee.getShop_id()!=null){
            // 如果是店铺员工就将shopId赋值到查询语句当中
            searchMasterMsgQuery.setShopId(employee.getShop_id());
        }
        // 待处理的宠物state（状态）都是0
        searchMasterMsgQuery.setState(0);
        // 调用父类方法将返回的查询信息封装到pageList中
        PageList<SearchMasterMsg> searchMasterMsgPageList = super.queryList(searchMasterMsgQuery);
        return searchMasterMsgPageList;
    }

    /**
     * 已处理的宠物信息的查询
     * @param searchMasterMsgQuery
     * @param logininfoId
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery searchMasterMsgQuery, Long logininfoId) {
        // 通过登录用户的id查询员工
        Employee employee = employeeMapper.findByLogininfoId(logininfoId);
        // 判断是否是店铺员工
        if (employee.getShop_id()!=null){
            // 店铺员工将店铺的id作为（高级）查询条件
            searchMasterMsgQuery.setShopId(employee.getShop_id());
        }
        // 未处理宠物的状态是1
        searchMasterMsgQuery.setState(1);
        // 调用父类方法将返回数据封装到pageList中
        PageList<SearchMasterMsg> searchMasterMsgPageList = super.queryList(searchMasterMsgQuery);
        return searchMasterMsgPageList;
    }

    /**
     * 处理宠物信息的业务方法
     * @param pet
     * @param id
     */
    @Override
    public void handle(Pet pet, Long id) {
        // 获取宠物信息中寻主表中的id，为了改变状态
        Long smmId = pet.getSearch_master_msg_id();
        // 通过寻主的id找到寻主表中的信息
        SearchMasterMsg smm = searchMasterMsgMapper.loadById(smmId);
        // 改变表中的状态
        smm.setState(1);
        searchMasterMsgMapper.update(smm);
        pet.setUser(smm.getUser());
        pet.setUser_id(smm.getUser_id());
        //  将pet和petDetail中存入数据库，调用单独的service层
        petService.insert(pet);
    }
//    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt) {
//        PetAcquisitionOrder order = new PetAcquisitionOrder();
//        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
//        order.setState(0);//待支付
//        order.setPrice(pet.getCostprice());
//        order.setAddress(adopt.getAddress());
//        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
//        order.setOrderSn(orderSn);
//        order.setPet_id(pet.getId());
//        order.setUser_id(adopt.getUser_id());
//        order.setPaytype(0);
//        order.setShop_id(pet.getShop_id());
//        order.setEmp_id(EmployeeContext.getLoginEployee().getId());
//        return order;
//    }
}
